package com.hyts.bmsp.web.dictionary;

import java.util.ArrayList;
import java.util.List;

import com.hyts.bmsp.controller.BaseController;
import com.hyts.bmsp.controller.WebController;
import com.hyts.bmsp.bean.ResultJsonBean;
import com.hyts.bmsp.model.dictionary.DictionaryTypeModel;
import com.hyts.bmsp.model.dictionary.DictionaryTypeQueryDTO;
import com.hyts.bmsp.service.dictionary.DictionaryTypeService;
import com.hyts.bmsp.service.dictionary.DictionaryTypeServiceImpl;
import com.hyts.bmsp.tool.ClassRuleUtil;
import com.hyts.bmsp.tool.IdUtil;
import org.apache.shiro.util.CollectionUtils;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Page;

public class DictionaryTypeController extends BaseController {

	/**
	 * @fieldName: service
	 * @fieldType: DictionaryTypeService
	 * @Description: 数据类型业务逻辑接口实现类
	 */
	private DictionaryTypeService service = new DictionaryTypeServiceImpl();
	
	
	public void index()
	{
	    toJsp("/admin/dictionary/dictionary_type_index");
	}

	
	/**
	 * @Title: loadIndexDataList
	 * @Description: 查询加载首页数据信息列表
	 * @return: void
	 */
	public void loadIndexDataList(){
		
		DictionaryTypeQueryDTO dto = new DictionaryTypeQueryDTO();
		ResultJsonBean result = new ResultJsonBean(true,"查询成功!");
		try {
			this.injectRequestParamToObj(dto);
			//执行查询操作
			Page<DictionaryTypeModel> pageInfo = service.findDictionaryTypeList(dto);
			if(!CollectionUtils.isEmpty(pageInfo.getList()))
			{
				List<DictionaryTypeQueryDTO> viewDTO = new ArrayList<DictionaryTypeQueryDTO>(pageInfo.getList().size());
				for(DictionaryTypeModel model:pageInfo.getList()){
					DictionaryTypeQueryDTO dto3 = ClassRuleUtil.db2JavaClass(model, new DictionaryTypeQueryDTO());
					viewDTO.add(dto3);
				}
				result = new ResultJsonBean(true,"查询成功");
				JSONObject result2 = new JSONObject();
				result2.put("data", viewDTO);
				result2.put("totalRow", pageInfo.getTotalRow());
				result2.put("totalPage", pageInfo.getTotalPage());
				result.setEntity(result2);
			}
			/*else
			{
				result = new ResultJsonBean(false,"查询字典类型失败!");
			}*/
		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 toJson(result);
	}
	
	public void add()
	{
	    toJsp("/admin/dictionary/dictionary_type_add");
	}
	
	/**
	 * @Title: doAdd
	 * @Description: 执行新增方法：进行保存数据字典类型方法操作
	 * @return: void
	 */
	public void doAdd(){
		DictionaryTypeQueryDTO dto = new DictionaryTypeQueryDTO();
		ResultJsonBean result = new ResultJsonBean(true,"保存字典类型成功!");
		try {
			this.injectRequestParamToObj(dto);
			dto.setId(IdUtil.getUUIDIngnoreToken());
			boolean flag = this.service.addDicTypeModel(dto);
			if(!flag)
			{
				result = new ResultJsonBean(false,"保存字典类型失败!");
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
			result = new ResultJsonBean(false,"保存字典类型失败!");
		}
		this.toJson(result);
	}

	/**
	 * @Title: remove
	 * @Description: 删除数据字典类型信息数据，真删除
	 * @return: void
	 */
	public void remove()
	{
		DictionaryTypeQueryDTO dto = new DictionaryTypeQueryDTO();
		ResultJsonBean result = new ResultJsonBean(true,"删除字典类型成功!");
		try {
			this.injectRequestParamToObj(dto);
			boolean flag = this.service.deleteDicTypeModel(dto);
			if(!flag)
			{
				result = new ResultJsonBean(false,"删除字典类型失败!");
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
			result = new ResultJsonBean(false,"删除字典类型失败!");
		}
		this.toJson(result);
	}
	
	/**
	 * @Title: edit
	 * @Description: 跳转到编辑页面
	 * @return: void
	 */
	public void edit()
    {
	    toJsp("/admin/dictionary/dictionary_type_edit");
	}
	
	/**
	 * @Title: load
	 * @Description: 加载服务信息
	 * @return: void
	 */
	public void load()
	{
		String id = this.getPara("id");
		ResultJsonBean result = new ResultJsonBean(true,"加载数据类型信息成功!");
		if(StringUtils.isEmpty(id))
		{
			result = new ResultJsonBean(false,"加载数据类型信息失败!");
			this.toJson(result);
			return ;
		}
		DictionaryTypeQueryDTO model = new DictionaryTypeQueryDTO();
		model.setId(id);
		DictionaryTypeModel typeModel = this.service.loadDicTypeModel(model);
		DictionaryTypeQueryDTO dto3;
		try {
			dto3 = ClassRuleUtil.db2JavaClass(typeModel, new DictionaryTypeQueryDTO());
			result.setEntity(dto3);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
			result = new ResultJsonBean(false,"加载数据类型信息失败!");
		}
		this.toJson(result);
	}
	
	/**
	 * @Title: doEdit
	 * @Description: 执行编辑操作
	 * @return: void
	 */
	public void doEdit()
	{
		DictionaryTypeQueryDTO dto = new DictionaryTypeQueryDTO();
		ResultJsonBean result = new ResultJsonBean(true,"修改字典类型成功!");
		try {
			this.injectRequestParamToObj(dto);
			dto.setId(this.getPara("id"));
			boolean flag = this.service.editDicTypeModel(dto);
			if(!flag)
			{
				result = new ResultJsonBean(false,"修改字典类型失败!");
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
			result = new ResultJsonBean(false,"修改字典类型失败!");
		}
		this.toJson(result);
	}
}
